<template>
  <div class="deliveryFrom-table-wrap">
    <el-table border :data="tableData" style="width: 100%">
      <el-table-column label="下单时间" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{
            scope.row.orderTime | formatDateYMD
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单号" width="200">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.serialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单人" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.orderUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="200">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.prodName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司" width="350">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.cusName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品数量" width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.prodNum }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="tag" label="订单状态" width="150">
        <template slot-scope="scope">
          <span style="margin-left: 10px">
            <el-tag
              :type="
                scope.row.status === 0
                  ? 'danger'
                  : scope.row.status === 10
                  ? 'warning'
                  : scope.row.status === 20
                  ? ''
                  : 'success'
              "
              disable-transitions
              >{{ tag(scope.row.status) }}</el-tag
            >
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- openDelivery -->
          <!-- handleEdit(scope.$index, scope.row) -->
          <el-button
            size="mini"
            @click="
              scope.row.status === 0
                ? openDelivery(scope.row)
                : scope.row.status === 10
                ? takeGoods(scope.row)
                : scope.row.status === 20
                ? openDetail(scope.row,true)
                : handleEdit(scope.$index, scope.row)
            "
            >{{
              scope.row.status === 0
                ? "发货"
                : scope.row.status === 10
                ? "收货"
                : scope.row.status === 20
                ? "评价"
                : "查看评价"
            }}</el-button
          >
          <el-button
            type="primary"
            plain
            size="mini"
            @click="openDetail(scope.row,false)"
            >查看订单详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getOrderInfoApi, getOrderTakeApi,getEvaluateDetailApi } from "@/http/api.js";
export default {
  props: {
    tableData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      // 0:未发货,10:未收货,20:未评价,30:已评价
      filters: [
        { text: "未发货", value: 0 },
        { text: "未收货", value: 10 },
        { text: "未评价", value: 20 },
        { text: "已评价", value: 30 },
      ],
    };
  },
  methods: {
    tag(val) {
      let idx = this.filters.findIndex((e) => e.value == val);
      let text = this.filters[idx].text;
      return text;
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    openDelivery(info) {
      let d = new Date();
      info.logisticsCode = d.getTime() + `${info.serialNo.slice(0, 2)}`;
      this.$store.commit("delivery/changeDialogShow", true);
      this.$store.commit("delivery/setInfo", info);
    },
    openDetail(info,isEvaluation) {
      this.$store.commit("deliveryDetail/changeDialogShow", true);
      this.getOrderInfo(info.serialNo,isEvaluation);
    },
    // 获取订单详情
    async getOrderInfo(serialNo,isEvaluation) {
      let res = await getOrderInfoApi(serialNo);
      this.$store.commit("deliveryDetail/setInfo", res.data);
      this.$store.commit("deliveryDetail/isEvaluation",isEvaluation)
      if(isEvaluation) return
      let res1 = await getEvaluateDetailApi(serialNo)
      console.log(res1);
    },
    // 收货
    takeGoods(val) {
      console.log("收货", val);
      this.$confirm("是否确认收货?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "收货成功!",
          });
          this.getOrderTake(val.serialNo);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消收货",
          });
        });
    },
    // 确认收货
    async getOrderTake(serialNo) {
      try {
        let res = await getOrderTakeApi(serialNo);
        if (res.errCode == 0) {
          console.log(res);
          this.$emit("reSend");
        }
      } catch (e) {
        this.$message({
          type: "info",
          message: "已收货",
        });
      }
    },
    // 评价
  },
};
</script>
 
<style lang="less" scoped>
</style>